import { createRouter, createWebHistory } from 'vue-router'
import loginView from '@/views/login/index.vue'
import homeView from '@/views/home/index.vue'
import cardView from '@/views/home/card.vue'
import xrView from '@/views/home/xr.vue'
import bdView from '@/views/home/bd.vue'
import ybp1View from '@/views/home/ybp1.vue'
import NotFound from '@/views/404.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/home/xr' },
    { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
    {path: '/login',name: 'login',component: loginView,},
    {path: '/home',name: 'home',component: homeView,
      children:[
    { path: 'card', name: 'card', component: cardView },
    { path: 'xr', name: 'xr', component: xrView },
    { path: 'bd', name: 'bd', component: bdView },
    { path: 'ybp1', name: 'ybp1', component: ybp1View },
      ]
    },
  ],
})
router.beforeEach((to,from,next)=>{
  if(to.fullPath !='/login'){
    let token=localStorage.getItem('token')
    if(!token){
      next('/login')
    }else{
      next()
    }
  }else{
    next()
  }
})
export default router
